Computational Vision
U. Minn. Psy 5036W

Assignment #0: Getting started with Mathematica

Your assignment is to simply work through this notebook. Evaluate all the executable cells. Feel free to change, play with values or functions. Once done, email your notebook to the TA.

Getting started with Mathematica

Introduction

To get an overview with audio, go to the screencast:

http://www.wolfram.com/broadcast/screencasts/handsonstart/

or for version 9:

http://www.wolfram.com/support/learn/get-started-with-mathematica/

Go to the Help menu in Mathematica. Go to Documentation Center, and from there to Getting Started Videos or Find Your Learning Path.

You can read Mathematica files free with Mathematica Player.

Front-end and Notebooks: Organize, outline, document. Program, evaluations, data all in one place
Kernel: Separate program does the calculations

Some practice

Numerical Calculations. You can do arithmetic. For example, type 5+7 as shown in the cell below, and then hit the "enter" key. Note that if you
try division, e.g. 2/3, you get the exact answer back. To get a decimal approximation, type N[2/3].

5+7

2/3

N[2/3]

You can go back and select an expression by clicking on the brackets on the far right. These brackets serve to organize text and calculations into a Notebook with outlining features. You can group or ungroup cells for text, graphs, and expressions in various ways to present your calculations. Explore these options under Cell in the menu.  You can see the possible cell types under the Style menu.

Try some other operations, 5^3, 4*3 (note that 4 3, where a space separates the digits is also interpreted as multiplication.

Evaluate 4*3

Compare with 4 3 (i.e. 4 followed by a space, and then 3).

Seems pretty dumb so far...but you can see that Mathematica's default handling of arithmetic is special:

Compare the square roots of: 12345678987654321 and 12345678987654321.0

Compare (2^.000000000001)^1000000000000 with (2^(1/1000000000000))^1000000000000

If you don't explicitly tell Mathematica that your expressions involving floating point numbers, it will treat the numbers as true integers. And in general, Mathematica will do symbolic processing as a default, and assume you want as general an answer as possible. Because symbolic processing demands considerably more computer resources than numerical processing, this result in annoying consequences, like slow or no responsivity. So remember to include a decimal point somewhere, or use N[] to force a floating point representation, and consequently numerical computations.

Front-end stuff

There's a lot to explore stylistically in Notebooks, but one of the most common things you will do is to select and manipulate the brackets on the far right. You can go back and select an expression by clicking on the brackets on the far right. These brackets are features of the user interface and serve to organize text and calculations into a Notebook with outlining features. You can group or ungroup cells for text, graphs, and expressions in various ways to present your calculations. Explore these options under Cell in the menu.  You can see the possible cell types under the Style menu.

By ending an expression with ; you can suppress the output.

(3/4)/6;
(3 4)/6;

a=Table["hi",{50000}]

The most recent result of a calculation is given by %, the one before by %%, and so forth.  Try it on the previous two outputs

Built-in functions

Mathematica has a very large library of built-in functions. They all begin with an uppercase letter and the arguments are enclosed by square brackets. Knowing that, you can often guess the form of a function.

Try taking the logarithm of 8.0

Did it return log to the base 10 or e? Check the definition by typing "?Log" or by typing "Log[E]" and "Log[10]"

You can get information more about a function, by clicking on the resulting link >>

Try Log[2,8]

You can get information about a function, e.g. for the exponential of a function, or for plotting graphs by selecting the function (e.g. Exp) and going to "Find Selected Function" in the Help menu. Or you can enter:

?Exp

?Plot

If you type two question marks before a function, ??Plot, you'll get more information. Try it. What does the Random function do?

Lists

We will uses lists a lot. Lists are general in Mathematica. Their elements can be numbers, symbols, other lists, and a list can have mixed types.

{1.1, "hi", {Pi, 33/2}}

Mostly we will use lists to represent vectors and matrices. You can quickly make a list  using the Table function:

avector = Table[i^2, {i,1,6}]

amatrix = Table[N[Cos[Sqrt[i^2 + j^2]]],{i,-6,6},{j,-6,6}];

Remove the ; above to see what the list looks like in output form. 
We will use matrices (a list of lists of pixels) to represent images. We can visualize what this 2D list looks like using Image[]

Image[amatrix]

Defining your own functions

Let's illustrate function definition by building a really simple model of a neuron. We'll see the justification later.  Suppose a neuron takes a set of inputs, say x1, x2, x3,  (e.g. from the outputs of three other neurons in the network) and produces an output signal, call it y. For a so-called linear model, the output is the weighted sum of the inputs. We'll assume the weights are fixed and given by w1, w2, w3.

w1=-1; w2 = 2; w3=-1;

y[x1_,x2_,x3_]:= w1*x1 + w2*x2 +w3*x3;

(This code is pretty primitive and not very general--don't worry, we'll get more sophisticated later). 
The underscore in x1_ is important because it tells Mathematica that x1 represents a slot for a variable, not an expression.

:= vs.  =

Note that when defining a function, we used a colon followed by equals ( := ) instead of just an equals sign (=). When you use an equals sign, the value is calculated and assigned immediately. When there is a colon in front of the equals, the value is calculated only when called on later. So we use := for function definition because we need to define the function for later use and evaluation,  when we may have new values for its arguments.
A double equals (==) has yet another meaning and is used to represent a symbolic equation which evaluates to True if left and right hand sides are identical.

Let's define xv using :=, and xf using =

xv:=RandomInteger[10];
xf=RandomInteger[10];

Now evaluate r1 and r2 three times each. What is the difference between the two definitions?

{xv, xf}

..but if we evaluate xf = RandomInteger[10] again, we re-initialize it:

xf=RandomInteger[10]

xf=RandomInteger[10];
y[xf,xf,xf]

y[xv,xv,xv]

Why does our neuron model always output a zero when the inputs  all have the same value? What other family of inputs will all produce zero output? 
Hint: The weights we defined can be thought of as a discrete approximation to a 2nd derivative operator in differential calculus.

Graphics & more function definitions

Later on we'll  require defining a function that suppresses small outputs and "squashes" or clamps large outputs to a maximum level. Here is an example:

squash[x_] := N[1/(1 + Exp[-x+4])];

Also note that our squashing function was defined with N[]. Again, remember that Mathematica trys to keep everything exact as long as possible and thus will try to do symbol manipulation if we don't explicitly tell it that we want numerical representations and calculations.

Let's plot a graph of the squash function using the syntax we discovered above for -5<x<10

Plot[squash[x],{x,-5,10}]

We'll use this and similar functions to model the small-signal compression and large signal saturation characteristics of neural output.

Define a new function squashedExp[ ] that applies squash to an exponentiated value (i.e. takes Exp[x] as the argument of squash[ ])

squashedExp[x_]:=squash[Exp[x]];

Plot squashedExp[ ] for x going from -5 to 5

Plot[{Exp[x],squashedExp[x]},{x,-5,5}]

Even though Exp grows exponentially fast with x (by definition!), squash “keeps a lid” on it.

Ask Mathematica for the definition of squashedExp[ ]

It can be important to check your definitions like this. One reason is that, as we will see later, Mathematica definitions can be built up with multiple constraints. And sometimes you might add to a function unwittingly and it appears to misbehave. You can check your defintion by asking Mathematica for it.

Define a new version of the squash function to include a steepness term λ, and use Manipulate to control  λ

Mathematica 7 & 8 have very useful and easy-to-program GUI tools. One of the most common is   Manipulate[]

dsquash[x_,λ_] := N[1/(1 + Exp[(-x+4)/λ])];
Manipulate[Plot[dsquash[x,λ], {x, -5, 10}],{{λ,1},0.1,2}]

Here’s a 3D surface plot representing possible selectivities in the receptive field of a simple cell in the visual cortex:

Manipulate[Plot3D[Exp[-x^2-y^2]*Sin[a*x + b*y],{x,-2,2},{y,-2,2},PlotRange->{-1,1}],{a,1,6},{b,1,6}]

Try using your mouse and mouse button to rotate the above plot. What does the Option or Alt key do?

Image processing

You can drag an image into the argument slot

Image[];

You can select, copy, and the paste the image into other function, e.g. to extract the data in list form:

idata = ImageData[];

Images are often really big, so we can check the format and size of the image data using Dimensions[]

Dimensions[idata ]

So it isn’t a simple matrix. The first two entries tell us the number of rows and columns respectively. Note that although an image might look black and white, it’s pixels have still three values, one for each color channel. This is because it is in color format. We can convert it to black and white using ColorConvert[]

ColorConvert[,"Grayscale"]

Dimensions[ImageData[ColorConvert[,"Grayscale"]]]

Try dragging a new image into one of the above image functions.

Mathematica Demonstrations project

Check out the Demonstrations center at the Wolfram Mathematica site for some cool examples, such as:

Manipulate[Module[{bluecol,colfunc},
bluecol[n_]:=Blend[{Black,Blue,White},n];
If[usecol, colfunc=bluecol, colfunc=GrayLevel];

Graphics[{colfunc[left],Rectangle[{0,0},{3,3}],colfunc[right],Rectangle[{3,0},{6,3}],colfunc[mid],Rectangle[{1,1},{2,2}],Rectangle[{4,1},{5,2}]}]],{{left,.35,"left square"},.2,.8},{{right,.75,"right square"},.2,.8},{{mid,.5,"middle squares"},.2,.8},{{usecol, True,""},{True->"blues", False->"grays"},ControlPlacement->Bottom}]

"The Simultaneous Contrast Effect" from The Wolfram Demonstrations Project http://demonstrations.wolfram.com/TheSimultaneousContrastEffect/